package com.work.saturn.account;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.immo.libcommon.base.BaseBean;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.constant.RouterUtil;
import com.immo.libcommon.constant.SharePreferenceKey;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.utils.SharePreferenceUtils;
import com.work.saturn.R;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.immo.libcommon.utils.InputValidationUtils.inputIsEmpty;

/**
 * @author YaoChen
 * @content 创建Token
 */
@Route(path = RouterUtil.CREATE_TOKEN)
public class CreateTokenActivity extends BaseHeadActivity {
    /**
     * 代币名称
     */
    @BindView(R.id.token_name)
    EditText tokenName;
    /**
     * 代币单位
     */
    @BindView(R.id.token_symbol)
    EditText tokenSymbol;
    /**
     * 代币精度
     */
    @BindView(R.id.token_decimol)
    EditText tokenDecimol;
    /**
     * 代币金额
     */
    @BindView(R.id.token_supply)
    EditText tokenSupply;
    /**
     * 描述
     */
    @BindView(R.id.token_introduction)
    EditText tokenIntroduction;
    @BindView(R.id.token_gas_01)
    EditText mTokenGas01;
    @BindView(R.id.token_gas_02)
    EditText mTokenGas02;
    @BindView(R.id.token_Ram_01)
    EditText mTokenRam01;
    @BindView(R.id.token_Ram_02)
    EditText mTokenRam02;
    @BindView(R.id.token_main_Account)
    EditText mTokenMainAccount;
    @BindView(R.id.token_100_Account)
    EditText mToken100Account;
    @BindView(R.id.token_101_Account)
    EditText mToken101Account;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creat_token);
        ButterKnife.bind(this);
        //设置标题
        setTitle(getString(R.string.create_token));
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        return inputIsEmpty(this, tokenName.getText().toString(), getString(R.string.name_no_empty))
                && inputIsEmpty(this, tokenSymbol.getText().toString(), getString(R.string.symbol_no_empty))
                && inputIsEmpty(this, tokenDecimol.getText().toString(), getString(R.string.dcimol_no_empty))
                && inputIsEmpty(this, tokenSupply.getText().toString(), getString(R.string.supply_no_empty))
                && inputIsEmpty(this, mTokenGas01.getText().toString(), "信息填写不完整")
                && inputIsEmpty(this, mTokenGas02.getText().toString(), "信息填写不完整")
                && inputIsEmpty(this, mTokenRam01.getText().toString(), "信息填写不完整")
                && inputIsEmpty(this, mTokenRam02.getText().toString(), "信息填写不完整")
                && inputIsEmpty(this, mTokenMainAccount.getText().toString(), "信息填写不完整")
                && inputIsEmpty(this, mToken100Account.getText().toString(), "信息填写不完整")
                && inputIsEmpty(this, mToken101Account.getText().toString(), "信息填写不完整")
                && inputIsEmpty(this, tokenIntroduction.getText().toString(), getString(R.string.introduction_no_empty));
    }

    @OnClick(R.id.token_submit)
    public void onViewClicked() {
        if (isCheck()) {
            submitToken();
        }
    }

    /**
     * 创建token
     */
    private void submitToken() {
        HashMap<String, String> params = new HashMap<>();
        params.put("username", Objects.requireNonNull(SharePreferenceUtils.get(Objects.requireNonNull(this), SharePreferenceKey.USERNAME, "")).toString());
        params.put("id", Objects.requireNonNull(SharePreferenceUtils.get(Objects.requireNonNull(this), SharePreferenceKey.ID, "")).toString());
        params.put("name", tokenName.getText().toString());
        params.put("symbol", tokenSymbol.getText().toString());
        params.put("decimal", tokenDecimol.getText().toString());
        params.put("totalSupply", tokenSupply.getText().toString());
        params.put("gasMinimum", mTokenGas01.getText().toString());
        params.put("gasRate", mTokenGas02.getText().toString());
        params.put("ramMinimum", mTokenRam01.getText().toString());
        params.put("ramRate", mTokenRam02.getText().toString());
        params.put("mainAccount", mTokenMainAccount.getText().toString());
        params.put("tokenTaxAccount", mToken100Account.getText().toString());
        params.put("GZHTaxAccount", mToken101Account.getText().toString());
        params.put("introduction", tokenIntroduction.getText().toString());

        new NetworkRequest((object, data) -> {
            if (object instanceof BaseBean) {
                BaseBean baseBean = (BaseBean) object;
                toast(baseBean.getMessage());
                if (baseBean.isSuccess()) {
                    finish();
                }
            }
        }).requestPostKey(BaseUrl.CREATTOKEN,
                this, params, BaseBean.class, true, false);
    }
}
